package br.com.fogas.utils.task;

import br.com.fogas.utils.dialog.ProgressDialogBuilder;
import android.os.AsyncTask;
import android.os.Handler;

public class ServiceTask extends AsyncTask<Void, Void, Void> {

	private Handler handler;
	
	private ServiceTaskBusiness taskBusiness;
	
	private ProgressDialogBuilder dialog;
	
	public ServiceTask(ServiceTaskBusiness taskBusiness) {
		this.handler = new Handler();
		this.taskBusiness = taskBusiness;
	}
	
	public ServiceTask(ProgressDialogBuilder dialog, ServiceTaskBusiness taskBusiness) {
		this.handler = new Handler();
		this.dialog = dialog;
		this.taskBusiness = taskBusiness;
	}

	@Override
	protected void onPreExecute() {
		this.dialog.setHandler(handler).showDialog();
	}

	@Override
	protected void onPostExecute(Void voids) {
		boolean showing = this.dialog.getDialog().isShowing();
		if(showing) {
			this.dialog.closeDialog();
		}
	}

	@Override
	protected Void doInBackground(Void... voids) {
		taskBusiness.start(this);
		return null;
	}

	public ProgressDialogBuilder getDialog() {
		return dialog;
	}
}